Page({
  data: {
    age: null,
    weight: null,
    height: null,
    sex: '男',
    factor: 1.2,
    ageData: [],
    heightData: [],
    kcal: 0,
    kj:0,
    defalutAge:10,
    defalutWeight:40,
    defalutHeight:50,
  },
  onLoad: function (options) {
    this.setData({
      ageData: this.createNumber(12, 75),
      heightData: this.createNumber(120, 200),
      weightData: this.createNumber(30, 150)
    })
  },
  bindAgeChange(e) {
    let {
      ageData
    } = this.data;
    let index = e.detail.value;
    this.setData({
      age: ageData[index]
    })
  },
  bindWeightChange(e) {
    let {
      weightData
    } = this.data;
    let index = e.detail.value;
    this.setData({
      weight: weightData[index]
    })
  },
  bindHeightChange(e) {
    let {
      heightData
    } = this.data;
    let index = e.detail.value;
    this.setData({
      height: heightData[index]
    })
  },
  toCalculate() {
    let {
      age,
      weight,
      height,
      sex,
      factor
    } = this.data;
    if (!age) {
      wx.showToast({
        title: '请选择年龄',
        icon: "none"
      })
      return;
    }
    if (!weight) {
      wx.showToast({
        title: '请选择体重',
        icon: "none"
      })
      return;
    }
    if (!height) {
      wx.showToast({
        title: '请选择身高',
        icon: "none"
      })
      return;
    }
    let result = 0;
    if (sex == "男") {
      result = 10 * weight + 6.25 * height - 5 * age + 5
    } else {
      result = 10 * weight + 6.25 * height - 5 * age - 161
    }
    this.setData({
      kcal: (result * factor).toFixed(0),
      kj: ((result * factor) * 4.128).toFixed(0),
      sugar:(((result * factor) * 4.128)/16.74).toFixed(0),
      fat:(((result * factor) * 4.128)/32.23).toFixed(0)
    })
  },
  sexChange(e) {
    this.setData({
      sex: e.detail.value
    })
  },
  radioChange(e) {
    this.setData({
      factor: Number(e.detail.value)
    })

  },
  copy(){
    wx.setClipboardData({
      data: 'https://zhuanlan.zhihu.com/p/54495331',
      success (res) {
        wx.showToast({
          title: '链接复制成功'
        })
      }
    })
  },
  createNumber(min, max) {
    var arr = new Array();
    for (var i = min; i <= max; i++) {
      arr.push(i);
    }
    return arr
  },
  onShareAppMessage: function () {

  }
})